package edu.ustb.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import edu.ustb.domain.User;

import edu.ustb.mapper.UserMapper;
import edu.ustb.result.Result;
import edu.ustb.utils.FileUtil;
import edu.ustb.utils.MybatisUtil;


/**
 * Servlet implementation class UserLoginController
 */
@WebServlet("/user/login.do")
public class UserLoginController extends HttpServlet {
	
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String userCode = request.getParameter("userCode");
		
		System.out.println(phone+","+password+","+userCode);
		Result result = new Result();
		
		
		try {
			
		InputStream	is = Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
		SqlSession openSession = ssf.openSession();
		UserMapper mapper = openSession.getMapper(UserMapper.class);
			//UserMapper mapper = MybatisUtil.getMapper(UserMapper.class);
			System.out.println(mapper);
			User user = mapper.findByUserName(phone);
			System.out.println(user);
	
	 
	        //用户输入的验证码

	        //VerifyCodeImg存在session中的验证码
	        String rightCode=null;
	        Object code = request.getSession().getAttribute("code");
	        if(code!=null){
	            rightCode=(String) code;
	        }

	        //用户输入和生成验证码的比较，并生成不同的信息提示
	        if(userCode.equalsIgnoreCase(rightCode)){
	        	
	        	
	        	
	        	if(user.getUserPassword().equals(password)) {
	        		
	    			result.setCode(1);
	    			result.setMsg("登陆成功");
	    			response.getWriter().print(JSON.toJSONString(result));
	    			
	    		}else {
	    			result.setCode(2);
	    			result.setMsg("账号或密码错误");
	    			response.getWriter().print(JSON.toJSONString(result));
	    	}
	         
	        }else{System.out.println("55555555555555555555555555555555555555555555555555555555");
	        	result.setNode(3);
				result.setMsg("验证码错误");
				
				response.getWriter().print(JSON.toJSONString(result));
	        }
	

		//user.getUser_pass().equals(phone)
		
		} catch (IOException e) {
			
		e.printStackTrace();
		}
		

}
}